<?php
require_once("includes/connect.php");
require_once("includes/function.php");

$val = $_REQUEST["val"];
$tblname = $_REQUEST["tblname"];

$val1 = explode(",", $val);
$val1 = implode("','", $val1);

//echo $tblname;
?>

<script type="text/javascript">
    $(document).ready(function() {
        $("#i_date").datepicker({
            dateFormat: "yy-mm-dd",
            showButtonPanel: true
        });

        $("#save_menus").click(function() {
            event.preventDefault();

            var branch_name = $("#branch_name").val();
            var bank_name = $("#bank_name").val();
            var bank_type = $("#bank_type").val();
            var account_no = $("#account_no").val();
            var account_name = $("#account_name").val();
            var i_date = $("#i_date").val();
            var val = "<?php echo $val; ?>";
            var task = "add";
            var tblname = "<?php echo $tblname; ?>";

            switch (tblname) {
                case "tbl_pet":
                    var file_process = "trans_process.php";
                    //var pages = "pet_verify_approved";
                    break;

                case "tbl_petition":
                    var file_process = "trans_process_petition.php";
                    //var pages = "pet_verify_approved";
                    break;
            }

            $.ajax({
                type: "POST",
                url: file_process,
                data: {task: task
                    , branch_name: branch_name
                    , bank_name: bank_name
                    , bank_type: bank_type
                    , account_no: account_no
                    , account_name:account_name
                    , val: val
                    , i_date: i_date
                    , tblname: tblname},
                cach: false,
                error: function() {
                    alert("ERROR !!!!");
                },
                success: function(data) {
//                    alert(data);
                    if (confirm("คุณต้องการพิมพ์ใบโอนเงินในตอนนี้ หรือไม่")) {
                        window.open("trans_print.php?account_no=" + account_no + "&tblname=" + tblname, "_blank");
                        window.location.href = "index.php?pages=trans&tblname=" + tblname;
                    } else {

                        window.location.href = "index.php?pages=trans&tblname=" + tblname;
                    }
                }
            });


        });
    });
</script>
<?php
$db = new myDBS;
$db->selectDB("account");

$result = $db->showData("bank");

//$val = explode(",", $val);

switch ($tblname) {
    case "tbl_pet":
        $sql = "SELECT SUM(net) AS net "
                . " FROM tbl_pet "
                . " WHERE pet_id IN('$val1') ";

        break;

    case "tbl_petition":
        $sql = "SELECT SUM(inpNet) AS net "
                . " FROM tbl_petition "
                . " WHERE p_id IN('$val1') ";

    default:
        break;
}

//echo $sql;
$result1 = $db->runSQL($sql);
$rows2 = $db->fetch_object($result1);
?>

<p>
    <label>วันที่</label>
    <input class="span3" type="text" id="i_date" name="i_date" />
    <label>จำนวนเงิน</label>
    <input class="span3 text-right" type="text" id="sum" name="sum" value="<?php echo number_format($rows2->net, 2); ?>" readonly>

    <label>ธนาคาร</label>
    <select class="span5" id="bank_name" name="bank_name" >
        <?php
        while ($rows = $db->fetch_object($result)) {
            echo "<option>$rows->bank_name</option>";
        }
        ?>

    </select>
    <label>สาขา</label>
    <select class="span4" id="branch_name" name="branch_name" >
        <?php
        $result = $db->showData("branch");

        while ($rows = $db->fetch_object($result)) {
            echo "<option>$rows->branch_name</option>";
        }
        ?>
    </select>
    <label>ประเภทบัญชี</label>
    <select class="span4" id="bank_type" name="bank_type" >
        <?php
        
        $sql = "SELECT DISTINCT bank_type "
                . " FROM bank_account "
                . " ";
        $result = $db->runSQL($sql);

        while ($rows = $db->fetch_object($result)) {
            echo "<option>$rows->bank_type</option>";
        }
        ?>
    </select>
    <label>เลขที่บัญชี</label>
    <input class="span2" type="text" id="account_no" name="account_no" maxlength="13">

    <label>ชื่อบัญชี</label>
    <input class="span2" type="text" id="account_name" name="account_name" >
</p>
<div class="form-actions">
    <button class="btn btn-primary" id="save_menus">บันทึกและพิมพ์</button>
    <a class="btn close-dialog"><i class="icon-remove"></i> ยกเลิก</a>

</div>
